package cn.maihe.elg.operation.supports.guarantee;

import cn.maihe.elg.operation.supports.guarantee.enums.GuaranteeCode;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author WJH
 * @Date 2021/05/10
 */
@Component
public class GuaranteeServiceFactory {
    private static Map<GuaranteeCode, GuaranteeServiceInterface> guaranteeServiceMap = Maps.newHashMap();

    public GuaranteeServiceFactory(List<GuaranteeServiceInterface> guaranteeServiceInterfaces) {
        if (!CollectionUtils.isEmpty(guaranteeServiceInterfaces)) {
            guaranteeServiceInterfaces.forEach(ins -> {
                guaranteeServiceMap.put(ins.guaranteeCodeEnum(), ins);
            });
        }
    }

    public GuaranteeServiceInterface getGuaranteeService(GuaranteeCode guaranteeCode) {
        GuaranteeServiceInterface ins = guaranteeServiceMap.get(guaranteeCode);
        Assert.notNull(ins, () -> "查暂无此[" + guaranteeCode.getDesc() + "]通道服务实现");
        return ins;
    }

}
